#ifndef _MATMUL_KERNEL_H_
#define _MATMUL_KERNEL_H_

__global__ void MatrixMultiplySharedMemory(float* A, float* B, float* C, int ay, int by)
{
	int tileWidth = blockDim.x;
	
	//extern __shared__ float sharedArrays[];
	//float* Ashared = (float*) &sharedArrays;
	//float* Bshared = (float*) &Ashared[tileWidth*tileWidth];

	__shared__ float Ashared[4];
	__shared__ float Bshared[4];

	int tx = threadIdx.x;
	int ty = threadIdx.y;

	int row = (tileWidth * blockIdx.y) + ty;
	int col = (tileWidth * blockIdx.x) + tx;
	float cVal = 0;
	
	
	for (int i = 0; i < ceil((float)ay / tileWidth); i++)
	{
		Ashared[ty * tileWidth + tx] = A[row * ay + (i * tileWidth + tx)];
		Bshared[ty * tileWidth + tx] = B[(ty + (i * tileWidth)) * ay + col];

		__syncthreads();

		for(int j = 0; j < tileWidth; j++)
		{
			cVal += Ashared[ty * tileWidth + j] * Bshared[j * tileWidth + tx];
		}
		
		__syncthreads();
	}
	__syncthreads();
	C[row * ay + col] = cVal;
	
}

#endif // _MATMUL_KERNEL_H_
